﻿Imports Microsoft.Windows.Controls

Public Class EntryValidation

    Public Shared Function ValidatePassword(ByVal validatePasswordBox As PasswordBox) As Boolean
        If validatePasswordBox.Password.Length = 0 Then
            validatePasswordBox.ToolTip = "Password must be provided."
            validatePasswordBox.BorderBrush = New SolidColorBrush(Colors.Red)
            Return False
        Else
            validatePasswordBox.ClearValue(PasswordBox.BorderBrushProperty) 'New SolidColorBrush("#FF7F9DB9")
            validatePasswordBox.ToolTip = Nothing
            Return True
        End If

        Return True
    End Function

    Public Shared Function ValidateText(ByVal validateTextbox As TextBox, reset As Boolean) As Boolean
        If validateTextbox.Text.Length = 0 And Not reset Then
            validateTextbox.ToolTip = "This is a required field."
            validateTextbox.BorderBrush = New SolidColorBrush(Colors.Red)
            Return False
        Else
            validateTextbox.ClearValue(TextBox.BorderBrushProperty) 'New SolidColorBrush("#FF7F9DB9")
            validateTextbox.ToolTip = Nothing
            Return True
        End If
    End Function

    Public Shared Function ValidateDatePicker(ByVal validateTextbox As DatePicker) As Boolean
        If validateTextbox.SelectedDate Is Nothing Then
            validateTextbox.ToolTip = "This is a required field."
            validateTextbox.BorderBrush = New SolidColorBrush(Colors.Red)
            Return False
        Else
            If Not IsDate(validateTextbox.SelectedDate) Then
                validateTextbox.ToolTip = "Invalid date."
                validateTextbox.BorderBrush = New SolidColorBrush(Colors.Red)
                Return False
            Else
                validateTextbox.ClearValue(TextBox.BorderBrushProperty) 'New SolidColorBrush("#FF7F9DB9")
                validateTextbox.ToolTip = Nothing
                Return True
            End If
        End If
    End Function

    Public Shared Function ValidateCombo(ByVal validateCombobox As ComboBox, reset As Boolean) As Boolean

        If validateCombobox.SelectedIndex = -1 And Not reset Then
            'If validateCombobox.Text.Length = 0 Then
            validateCombobox.ToolTip = "This is a required field."
            validateCombobox.BorderThickness = New Thickness(2)
            validateCombobox.BorderBrush = New SolidColorBrush(Colors.Red)
            Return False
        Else
            validateCombobox.BorderThickness = New Thickness(0)
            validateCombobox.ClearValue(ComboBox.BorderBrushProperty) 'New SolidColorBrush("#FF7F9DB9")
            validateCombobox.ToolTip = Nothing
            Return True
        End If
    End Function

    Public Shared Function ValidateEditableCombo(ByVal validateCombobox As ComboBox, ByVal validateComboTextBox As TextBox) As Boolean

        If validateComboTextBox.Text = "" Then
            'If validateCombobox.Text.Length = 0 Then
            validateCombobox.ToolTip = "This is a required field."
            validateCombobox.BorderThickness = New Thickness(2)
            validateCombobox.BorderBrush = New SolidColorBrush(Colors.Red)
            Return False
        Else
            validateCombobox.BorderThickness = New Thickness(0)
            validateCombobox.ClearValue(ComboBox.BorderBrushProperty) 'New SolidColorBrush("#FF7F9DB9")
            validateCombobox.ToolTip = Nothing
            Return True
        End If
    End Function

    Public Shared Function ValidateIsNumeric(ByVal validateTextbox As TextBox) As Boolean

        Dim regex As New Text.RegularExpressions.Regex("^(\d|-)?(\d|,)*\.?\d*$")
        'regex that matches disallowed text 
        If Not regex.IsMatch(validateTextbox.Text) Or validateTextbox.Text.Length = 0 Then
            validateTextbox.ToolTip = "This is a numeric field."
            validateTextbox.BorderBrush = New SolidColorBrush(Colors.Red)
            Return False
        Else
            validateTextbox.ClearValue(TextBox.BorderBrushProperty) 'New SolidColorBrush("#FF7F9DB9")
            validateTextbox.ToolTip = Nothing
            Return True
        End If
    End Function
End Class
